import {iBindingEditorEntity} from "@peryl/react-cms-editor";
import {$dialog, $message, Button, Empty} from "plain-design";
import {doNothing} from "@peryl/utils/doNothing";

export function registryResumeEntity(option: { editorEntity: iBindingEditorEntity }) {

  const defaultDisplay = doNothing;
  const defaultEditor = doNothing;

  option.editorEntity.generalityConfig.registry(10, 'ResumeBasicInfoItem', {
    name: '简历基本信息子项',
    defaultDisplay, defaultEditor,
    defineFieldsConfig: [
      { name: '子项名称', field: 'label', entityCode: { sec: 'string' } },
      { name: '子项类型', field: 'type', entityCode: { sec: 'string' } },
      { name: '子项图标', field: 'icon', entityCode: { sec: 'string' } },
      { name: '子项值', field: 'val', entityCode: { sec: 'string' } },
    ],
  });

  option.editorEntity.generalityConfig.registry(10, 'ResumeBasicInfo', {
    name: '简历基本信息',
    defaultDisplay, defaultEditor,
    defineFieldsConfig: [
      { name: '标题', field: 'title', entityCode: { sec: 'string' } },
      { name: '子项数据', field: 'data', entityCode: { sec: 'Array', gcs: { T: { sec: 'ResumeBasicInfoItem' } } } },
    ],
  });

  option.editorEntity.generalityConfig.registry(10, 'ResumeEducationItem', {
    name: '简历教育经历子项',
    defaultDisplay, defaultEditor,
    defineFieldsConfig: [
      { name: '时间', field: 'time', entityCode: { sec: 'string' } },
      { name: '学校', field: 'school', entityCode: { sec: 'string' } },
      { name: '专业', field: 'program', entityCode: { sec: 'string' } },
      { name: '主修内容', field: 'content', entityCode: { sec: 'Array', gcs: { T: { sec: 'string' } } } },
    ],
  });

  option.editorEntity.generalityConfig.registry(10, 'ResumeEducation', {
    name: '简历教育经历',
    defaultDisplay, defaultEditor,
    defineFieldsConfig: [
      { name: '标题', field: 'title', entityCode: { sec: 'string' } },
      { name: '子项数据', field: 'data', entityCode: { sec: 'Array', gcs: { T: { sec: 'ResumeEducationItem' } } } },
    ],
  });

  option.editorEntity.generalityConfig.registry(10, 'ResumeExperienceItem', {
    name: '简历工作经历子项',
    defaultDisplay, defaultEditor,
    defineFieldsConfig: [
      { name: '时间', field: 'time', entityCode: { sec: 'string' } },
      { name: '公司/项目', field: 'company', entityCode: { sec: 'string' } },
      { name: '岗位名称', field: 'role', entityCode: { sec: 'string' } },
      { name: '职责内容', field: 'content', entityCode: { sec: 'Array', gcs: { T: { sec: 'string' } } } },
    ],
  });

  option.editorEntity.generalityConfig.registry(10, 'ResumeExperience', {
    name: '简历工作经历',
    defaultDisplay, defaultEditor,
    defineFieldsConfig: [
      { name: '标题', field: 'title', entityCode: { sec: 'string' } },
      { name: '子项数据', field: 'data', entityCode: { sec: 'Array', gcs: { T: { sec: 'ResumeExperienceItem' } } } },
    ],
  });

  option.editorEntity.generalityConfig.registry(10, 'ResumeExternalItem', {
    name: '简历其他内容子项',
    defaultDisplay, defaultEditor,
    defineFieldsConfig: [
      { name: '标题', field: 'title', entityCode: { sec: 'string' } },
      { name: '内容', field: 'content', entityCode: { sec: 'Array', gcs: { T: { sec: 'string' } } } },
    ],
  });

  option.editorEntity.generalityConfig.registry(10, 'Resume', {
    name: '简历信息',
    defineFieldsConfig: [
      { name: '简历标题', field: 'title', entityCode: { sec: 'string' } },
      { name: '主题颜色', field: 'primary', entityCode: { sec: 'color' } },
      { name: '次级颜色', field: 'secondary', entityCode: { sec: 'color' } },
      { name: '职业照片', field: 'avatar', entityCode: { sec: 'string' } },
      { name: '基本信息', field: 'basicInfo', entityCode: { sec: 'ResumeBasicInfo' } },
      { name: '教育经历', field: 'education', entityCode: { sec: 'ResumeEducation' } },
      { name: '工作经历', field: 'experience', entityCode: { sec: 'ResumeExperience' } },
      { name: '其他内容', field: 'externals', entityCode: { sec: 'Array', gcs: { T: { sec: 'ResumeExternalItem' } } } },
    ],
    defaultDisplay: ({ value }) => {
      if (!value) {
        return <Empty noData/>;
      }
      /*todo resume editor*/
      return (
        <span>简历：{value?.normalText}</span>
      );
    },
    defaultEditor: ({ value, onChange }) => {
      /*先弄的简单点直接编辑简历数据*/
      return (
        <Button mode="text" label="点击编辑详细信息" onClick={() => {
          $dialog({
            editType: 'textarea',
            editValue: JSON.stringify(value, null, 2),
            confirmButton: true,
            cancelButton: true,
            handleConfirm: ({ editValue }) => {
              try {
                const editData = editValue == null ? null : JSON.parse(String(editValue));
                onChange(editData);
              } catch (e) {
                $message.error(`请输入有效JSON格式字符串`);
                return false;
              }
            }
          });
        }}/>
      );
    }
  });
}
